package CommonGui;


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;


public class Translate implements ActionListener
{
	private Color btnColor;
	public JButton key[];
	public byte CAPS=1;
	public String st;
	public char symbol;
	public JTextField TextField;
	public JFormattedTextField formattedTextField;
	public boolean language=true;
	public boolean Reg=true;
	public int position;
	public int KBSwitch;                            // KBSwitch=0   ->  JTextfield
                                                    // KBSwitch=1   ->  FormattedTextField
	public Translate()
	{
		
	}
	
	public Translate(Color c)
	{
		this.btnColor=c;
	}
	
//--------------------------------------------------------------------sistem()-----------------------------------------------------------	
	public void sistem(String st){
		if (st.compareTo("BSpace")==0)
		{
			Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
	        JTextField txtF=(JTextField)component;
	        int caret=txtF.getCaretPosition();
	        String text=txtF.getText();		        
	        txtF.setText(text.substring(0,caret-1) + text.substring(caret));
	        txtF.setCaretPosition(caret-1);
		}
		if (st.compareTo("SPACE")==0)
		{			 
		        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		        JTextField txtF=(JTextField)component;
		        int caret=txtF.getCaretPosition();
		        String text=txtF.getText();
		        txtF.setText(text.substring(0,caret) +" "+ text.substring(caret));
		        txtF.setCaretPosition(caret+1);
		}
		if (st.compareTo("CAPS")==0){
			if (Reg) {
				Reg=false;
				if (language){
					CAPS=0;
				
				}
				else {
					CAPS=3;
			
				}
				
			}
			else {
				
				Reg=true;
				if (language){
					CAPS=1;
					}
				else {
					CAPS=2;
			
				}
			}
		}
		if (st.compareTo("EN/HE")==0){
			if (language) {
				language=false;
				if (Reg){
					CAPS=2;
			
				}
				else {
					CAPS=3;
				
				}
				
			}
			else {
				
				language=true;
				if (Reg){
					CAPS=1;
				
				}
				else {
					CAPS=0;
								}
			}
		}
		
		init2();
		
	}
//==================================end of sistem() =================================================================	
	
	
	
	
	
	


//------------------------------------init()------------------------------------------------------------------------------	
public void init() {

			key[0]=new JButton2("`");
			key[1]=new JButton2("1");
			key[2]=new JButton2("2");
			key[3]=new JButton2("3");
			key[4]=new JButton2("4");
			key[5]=new JButton2("5");
			key[6]=new JButton2("6");
			key[7]=new JButton2("7");
			key[8]=new JButton2("8");
			key[9]=new JButton2("9");
			key[10]=new JButton2("0");
			key[11]=new JButton2("-");
			key[12]=new JButton2("=");
			key[13]=new JButton2("BSpace");
			key[14]=new JButton2("q");
			key[15]=new JButton2("w");
			key[16]=new JButton2("e");
			key[17]=new JButton2("r");
			key[18]=new JButton2("t");
			key[19]=new JButton2("y");
			key[20]=new JButton2("u");
			key[21]=new JButton2("i");
			key[22]=new JButton2("o");
			key[23]=new JButton2("p");
			key[24]=new JButton2("[");
			key[25]=new JButton2("]");
			key[26]=new JButton2("a");
			key[27]=new JButton2("s");
			key[28]=new JButton2("d");
			key[29]=new JButton2("f");
			key[30]=new JButton2("g");
			key[31]=new JButton2("h");
			key[32]=new JButton2("j");
			key[33]=new JButton2("k");
			key[34]=new JButton2("l");
			key[35]=new JButton2(";");
			key[36]=new JButton2("'");
			key[37]=new JButton2("\\");
			key[38]=new JButton2("z");
			key[39]=new JButton2("x");
			key[40]=new JButton2("c");
			key[41]=new JButton2("v");
			key[42]=new JButton2("b");
			key[43]=new JButton2("n");
			key[44]=new JButton2("m");
			key[45]=new JButton2(",");
			key[46]=new JButton2(".");
			key[47]=new JButton2("/");
			key[48]=new JButton2("SPACE");
			key[49]=new JButton2("CAPS");
			key[50]=new JButton2("EN/HE");
	
	}



//========================================end of init() =======================================================================




//-------------------------------------------init2() ------------------------------------------------------------------------------

public void init2() {
	if (CAPS==0) {
		key[0].setText("~");
		key[1].setText("!");
		key[2].setText("@");
		key[3].setText("#");
		key[4].setText("$");
		key[5].setText("%");
		key[6].setText("^");
		key[7].setText("&");
		key[8].setText("*");
		key[9].setText("(");
		key[10].setText(")");
		key[11].setText("_");
		key[12].setText("+");
		key[13].setText("BSpace");
		key[14].setText("Q");
		key[15].setText("W");
		key[16].setText("E");
		key[17].setText("R");
		key[18].setText("T");
		key[19].setText("Y");
		key[20].setText("U");
		key[21].setText("I");
		key[22].setText("O");
		key[23].setText("P");
		key[24].setText("{");
		key[25].setText("}");
		key[26].setText("A");
		key[27].setText("S");
		key[28].setText("D");
		key[29].setText("F");
		key[30].setText("G");
		key[31].setText("H");
		key[32].setText("J");
		key[33].setText("K");
		key[34].setText("L");
		key[35].setText(":");
		key[36].setText("\"");
		key[37].setText("|");
		key[38].setText("Z");
		key[39].setText("X");
		key[40].setText("C");
		key[41].setText("V");
		key[42].setText("B");
		key[43].setText("N");
		key[44].setText("M");
		key[45].setText("<");
		key[46].setText(">");
		key[47].setText("?");
		key[48].setText("SPACE");
		key[49].setText("CAPS");
		key[50].setText("EN/HE");
	}
	if (CAPS==1) {
		key[0].setText("`");
		key[1].setText("1");
		key[2].setText("2");
		key[3].setText("3");
		key[4].setText("4");
		key[5].setText("5");
		key[6].setText("6");
		key[7].setText("7");
		key[8].setText("8");
		key[9].setText("9");
		key[10].setText("0");
		key[11].setText("-");
		key[12].setText("=");
		key[13].setText("BSpace");
		key[14].setText("q");
		key[15].setText("w");
		key[16].setText("e");
		key[17].setText("r");
		key[18].setText("t");
		key[19].setText("y");
		key[20].setText("u");
		key[21].setText("i");
		key[22].setText("o");
		key[23].setText("p");
		key[24].setText("[");
		key[25].setText("]");
		key[26].setText("a");
		key[27].setText("s");
		key[28].setText("d");
		key[29].setText("f");
		key[30].setText("g");
		key[31].setText("h");
		key[32].setText("j");
		key[33].setText("k");
		key[34].setText("l");
		key[35].setText(";");
		key[36].setText("'");
		key[37].setText("\\");
		key[38].setText("z");
		key[39].setText("x");
		key[40].setText("c");
		key[41].setText("v");
		key[42].setText("b");
		key[43].setText("n");
		key[44].setText("m");
		key[45].setText(",");
		key[46].setText(".");
		key[47].setText("/");
		key[48].setText("SPACE");
		key[49].setText("CAPS");
		key[50].setText("EN/HE");
	}
	if (CAPS==2) {
		key[0].setText("¸");
		key[1].setText("1");
		key[2].setText("2");
		key[3].setText("3");
		key[4].setText("4");
		key[5].setText("5");
		key[6].setText("6");
		key[7].setText("7");
		key[8].setText("8");
		key[9].setText("9");
		key[10].setText("0");
		key[11].setText("-");
		key[12].setText("=");
		key[13].setText("BSpace");
		key[14].setText("/");
		key[15].setText("'");
		key[16].setText("ק");
		key[17].setText("ר");
		key[18].setText("א");
		key[19].setText("ט");
		key[20].setText("ו");
		key[21].setText("ן");
		key[22].setText("ם");
		key[23].setText("פ");
		key[24].setText("]");
		key[25].setText("]");
		key[26].setText("ש");
		key[27].setText("ד");
		key[28].setText("ג");
		key[29].setText("כ");
		key[30].setText("ע");
		key[31].setText("י");
		key[32].setText("ח");
		key[33].setText("ל");
		key[34].setText("ך");
		key[35].setText("ף");
		key[36].setText(",‎");
		key[37].setText("\\");
		key[38].setText("ז");
		key[39].setText("ס");
		key[40].setText("ב");
		key[41].setText("ה");
		key[42].setText("נ");
		key[43].setText("מ");
		key[44].setText("צ");
		key[45].setText("ת");
		key[46].setText("‏ץ");
		key[47].setText(".");
		key[48].setText("SPACE");
		key[49].setText("CAPS");
		key[50].setText("EN/HE");
	}
	if (CAPS==3) {
		key[0].setText("~");
		key[1].setText("!");
		key[2].setText("@");
		key[3].setText("#");
		key[4].setText("$");
		key[5].setText("%");
		key[6].setText("^");
		key[7].setText("&");
		key[8].setText("*");
		key[9].setText("(");
		key[10].setText(")");
		key[11].setText("_");
		key[12].setText("+");
		key[13].setText("BSpace");
		key[14].setText("/");
		key[15].setText("'");
		key[16].setText("ק");
		key[17].setText("ר");
		key[18].setText("א");
		key[19].setText("ט");
		key[20].setText("ו");
		key[21].setText("ן");
		key[22].setText("ם");
		key[23].setText("פ");
		key[24].setText("]");
		key[25].setText("]");
		key[26].setText("ש");
		key[27].setText("ד");
		key[28].setText("ג");
		key[29].setText("כ");
		key[30].setText("ע");
		key[31].setText("י");
		key[32].setText("ח");
		key[33].setText("ל");
		key[34].setText("ך");
		key[35].setText("ף");
		key[36].setText(",‎");
		key[37].setText("\\");
		key[38].setText("ז");
		key[39].setText("ס");
		key[40].setText("ב");
		key[41].setText("ה");
		key[42].setText("נ");
		key[43].setText("מ");
		key[44].setText("צ");
		key[45].setText("ת");
		key[46].setText("‏ץ");
		key[47].setText(".");
		key[48].setText("SPACE");
		key[49].setText("CAPS");
		key[50].setText("EN/HE");
	}
	
}


//====================================================end of init2() ========================================================================



//-------------------------------------------------------listen() ------------------------------------------------------------------------------
public void listen() 
{
		key[0].addActionListener(this);
		key[1].addActionListener(this);
		key[2].addActionListener(this);		
		key[3].addActionListener(this);	
		key[4].addActionListener(this);		
		key[23].addActionListener(this);		
		key[5].addActionListener(this);		
		key[6].addActionListener(this);		
		key[7].addActionListener(this);		
		key[8].addActionListener(this);		
		key[9].addActionListener(this);		
		key[10].addActionListener(this);		
		key[11].addActionListener(this);		
		key[12].addActionListener(this);	
		//key[13].addActionListener(this);	
		key[14].addActionListener(this);
		key[15].addActionListener(this);		
		key[16].addActionListener(this);		
		key[17].addActionListener(this);	
		key[18].addActionListener(this);	
		key[19].addActionListener(this);	
		key[20].addActionListener(this);	
		key[21].addActionListener(this);	
		key[22].addActionListener(this);		
		key[24].addActionListener(this);	
		key[25].addActionListener(this);	
		key[26].addActionListener(this);	
		key[27].addActionListener(this);	
		key[28].addActionListener(this);	
		key[29].addActionListener(this);		
		key[30].addActionListener(this);
		key[31].addActionListener(this);
		key[32].addActionListener(this);		
		key[33].addActionListener(this);		
		key[34].addActionListener(this);
		key[35].addActionListener(this);
		key[36].addActionListener(this);
		key[37].addActionListener(this);
		key[38].addActionListener(this);
		key[39].addActionListener(this);
		key[40].addActionListener(this);
		key[41].addActionListener(this);
		key[42].addActionListener(this);
		key[43].addActionListener(this);
		key[44].addActionListener(this);
		key[45].addActionListener(this);
		key[46].addActionListener(this);
		key[47].addActionListener(this);
		
		key[13].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				sistem(key[13].getText());
			}
		});
		
		key[48].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				sistem(key[48].getText());
			}
		});

		key[49].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				sistem(key[49].getText());
			}
		});

		key[50].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				sistem(key[50].getText());
			}
		});
			
};






	/*public void Write(char symbols) {
		if (KBSwitch==0)
		{
		if(TextField!=null)
		{
		String txt=TextField.getText();
		String begin=txt.substring(0, position);
		String end=txt.substring(position, txt.length());
		String txt2=begin +symbols + end;
		this.position++;	
		TextField.setText(txt2);
		}
		}
		
		if (KBSwitch==1)
		{
		if(formattedTextField!=null)
		{
		
			if (position==4)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=txt.substring(0, 3)+symbols;
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position++;
			}
			
			
			

			if (position==3)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=txt.substring(0, 2)+symbols+txt.substring(3, 4);
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position++;
			}
			
			
			
			if (position==1)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=txt.substring(0, 1)+symbols+txt.substring(2, 4);
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position=position+2;
			}
			
			
			if (position==0)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=symbols+txt.substring(1, 4);
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position++;
			}
			
			
			
			
		}
		}
		
		
		
	};*/












	 public void actionPerformed(ActionEvent e)
	    {
	        String actionCommand = e.getActionCommand();
	        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
	        JTextField txtF=(JTextField)component;
	        int caret=txtF.getCaretPosition();
	        String text=txtF.getText();
	        txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
	        txtF.setCaretPosition(caret+1);
	        //txt.setText(txt.getText() + actionCommand);
	    }
	
	public class JButton2 extends JButton
	{
		public JButton2(String str)
		{			
			super(str);
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			int x=screenSize.width;
			int y=screenSize.height;
			if(btnColor!=null)
	        {
	        	this.setBackground(btnColor);
	        }
			this.setFocusable(false);
			this.setFocusPainted(false);
			//this.setPreferredSize(new Dimension(x/10, y/5));
	        Font font = this.getFont();
	        float size = font.getSize() + x/100;
	        this.setFont(font.deriveFont(size));
		}
	}

}